package ywh.utils
{
	
	/**
	 * 历史管理器；
	 * @author WilliWHYang
	 */
	public class HistoryManager
	{
		private var _history:Array;
		private var _index:int;
		
		public function HistoryManager()
		{
			clear();
		}
		
		/**
		 * 清空历史;
		 */
		public function clear():void
		{
			_history = [];
			_index = -1;
		}
		
		/**
		 * 下一步;
		 * @return
		 */
		public function next():*
		{
			if (_index < _history.length - 1 && _index > -2)
			{
				return _history[++_index];
			}
			return null;
		}
		
		/**
		 * 上一步;
		 * @return
		 */
		public function prev():*
		{
			if (_index > 0 && _index <= _history.length)
			{
				return _history[--_index];
			}
			return null;
		}
		
		/**
		 * 添加到历史
		 * @param	data 可以追溯的数据;
		 */
		public function add(data:*):void
		{
			if (_index != _history.length - 1)
			{
				_history.splice(_index + 1, _history.length - _index);
			}
			_history.push(data);
			_index = _history.length - 1;
		}
		
		/**
		 * 历史中的长度;
		 */
		public function get length():int
		{
			return _history.length;
		}
		
		/**
		 * 当前步骤编号，从0开始;
		 */
		public function get index():int
		{
			return _index;
		}
	}

}